//
//  RBGoodsAddToothPopVC.m
//  zhongzhenglu
//
//  Created by mac on 2022/11/10.
//

#import "RBGoodsAddToothPopVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import "RBTextField.h"

@interface RBGoodsAddToothPopVC ()<UITextFieldDelegate>

@property(nonatomic, strong) NSMutableArray *itemViewsArrayM;

@end

@implementation RBGoodsAddToothPopVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.dismissOnTapBlank = YES;
    self.contentView = ({
        UIView *view = UIView.new;
        [view rbSetCorner:UIRectCornerTopLeft|UIRectCornerTopRight radius:Zoom(10)];
        view.backgroundColor = UIColor.whiteColor;
        [self.view addSubview:view];
        view;
    });
    
    [self.contentView view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:[RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:self.title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:CGFLOAT_MIN centerY:NO];
        [view h_view_addView:({
            UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:@"goodsDetailPramsClose"];
            [view extendResponseArea];
            [view rbAddTapGesWithTarget:self action:@selector(dismiss)];
            view;
        }) withHeight:Zoom(18) width:Zoom(18) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:CGFLOAT_MIN right:Zoom(12) centerY:YES];
        view;
    }) withHeight:Zoom(40) width:CGFLOAT_MIN top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:NO];
    [self.contentView view_addView:({
        UIScrollView *view = UIScrollView.new;
        [view rbSetContentWidth:kRBMainScreenWidth];
        for (NSInteger i = 0; i < self.options.count; i++)
        {
            [view addView:[self itemViewWithData:self.options[i] tag:i] withHeight:Zoom(40) width:CGFLOAT_MIN top:i?0:Zoom(10) bottom:0 left:0 right:0 centerX:0];
        }
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    [self.contentView view_addView:({
        UILabel *view = [RanTool labelWithFontSize:Zoom(14) weight:1 color:UIColor.whiteColor align:1 text:@"确定" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(20)];
        [view rbAddTapGesWithTarget:self action:@selector(confirmBtnTapped)];
        view;
    }) withHeight:Zoom(40) width:CGFLOAT_MIN top:Zoom(10) bottom:MAX(Zoom(12), kRBSafeBottomHeight) left:Zoom(12) right:Zoom(12) centerX:NO];
    
    self.view.backgroundColor = UIColor.clearColor;
    self.contentView.width = kRBMainScreenWidth;
    self.contentView.height = kRBMainScreenHeight*0.7;
    self.contentView.left = 0;
    self.contentView.top = kRBMainScreenHeight;
}

- (void)show
{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.contentView.bottom = kRBMainScreenHeight;
    }];
}

- (void)dismiss
{
    if(self.dismissBlock)
    {
        self.dismissBlock();
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.view.backgroundColor = UIColor.clearColor;
        self.contentView.top = kRBMainScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (void)configItemView: (UIView *)itemView withModel: (RBGoodsDetailSpecItemModel *)model
{
    UIImageView *selImgView = (UIImageView *)[itemView viewWithTag:301];
    if(model.selected)
    {
//        selImgView.backgroundColor = [RBThemeConfig buttonBlueColor];
//        [selImgView rbSetBorderWitdh:1 borderColor:UIColor.clearColor];
        selImgView.image = [UIImage imageNamed:@"addressListDefault"];
    }
    else
    {
        selImgView.image = [UIImage imageNamed:@"unsel"];
//        selImgView.backgroundColor = UIColor.clearColor;
//        [selImgView rbSetBorderWitdh:1 borderColor:[RBThemeConfig textLightGrayColor]];
    }
    RBTextField *textF = (RBTextField *)[itemView viewWithTag:303];
    textF.text = [NSString stringWithFormat:@"%zd", model.count];
}

#pragma mark - Network

- (void)itemSelectViewTapped: (UITapGestureRecognizer *)ges
{
    NSInteger tag = ges.view.superview.tag;
    RBGoodsDetailSpecItemModel *model = self.options[tag];
    model.selected = !model.selected;
    if(model.selected)
    {
        model.count = 1;
    }
    else
    {
        model.count = 0;
    }
    [self configItemView:self.itemViewsArrayM[tag] withModel:model];
}

- (void)itemMinusBtnTapped: (UITapGestureRecognizer *)ges
{
    NSInteger index = ges.view.superview.tag;
    RBGoodsDetailSpecItemModel *model = self.options[index];
    if(!model.selected || model.count <= 1)
    {
        return;
    }
    model.count--;
    [self configItemView:self.itemViewsArrayM[index] withModel:model];
}

- (void)itemPlusBtnTapped: (UITapGestureRecognizer *)ges
{
    NSInteger index = ges.view.superview.tag;
    RBGoodsDetailSpecItemModel *model = self.options[index];
    model.selected = YES;
    model.count++;
    [self configItemView:self.itemViewsArrayM[index] withModel:model];
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(RBTextField *)textField
{
    RBGoodsDetailSpecItemModel *model = textField.rbExData;
    NSInteger count = 0;
    if (textField.text.length)
    {
        count = textField.text.integerValue;
    }
    count = MAX(count,0);
    if(model.selected)
    {
        count = MAX(count,1);
    }
    model.count = count;
    model.selected = model.count > 0;
    [self configItemView:textField.superview.superview withModel:model];
}

#pragma mark - Event

- (void)confirmBtnTapped
{
    [self dismiss];
}

#pragma mark - Setter

#pragma mark - Getter

- (NSMutableArray *)itemViewsArrayM
{
    if(!_itemViewsArrayM)
    {
        self.itemViewsArrayM = NSMutableArray.array;
    }
    return _itemViewsArrayM;
}

- (UIView *)itemViewWithData: (RBGoodsDetailSpecItemModel *)model tag: (NSInteger)tag
{
    UIView *view = UIView.new;
    view.tag = tag;
    [self.itemViewsArrayM addObject:view];
    [view h_view_addView:({
        UIImageView *view = UIImageView.new;
        [view rbSetCornerRadius:Zoom(3)];
        [view extendResponseArea];
        [view rbAddTapGesWithTarget:self action:@selector(itemSelectViewTapped:)];
        view.tag = 301;
        view;
    }) withHeight:Zoom(12) width:Zoom(12) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(12) right:0 centerY:YES];
    [view h_view_addView:[RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:({
        NSMutableString *stringM = NSMutableString.string;
        [stringM appendString:model.spec_name];
        [stringM appendFormat:@"(¥%@)",model.spec_price];
        stringM;
    })] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(10) right:0 centerY:1];
    [view h_view_addView:({
        UIView *view = UIView.new;
        view.tag = tag;
        [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig lineGrayColor]];
        [view rbSetCornerRadius:Zoom(5)];
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:1 text:@"-"];
            view.tag = 302;
            [view rbAddTapGesWithTarget:self action:@selector(itemMinusBtnTapped:)];
            view;
        }) withHeight:CGFLOAT_MIN width:Zoom(30) top:0 bottom:0 left:0 right:0 centerY:0];
        [view h_view_addView:({
            RBTextField *view = RBTextField.new;
            view.rbExData = model;
            [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig lineGrayColor]];
            view.placeholder = @"0";
            view.textAlignment = NSTextAlignmentCenter;
            view.font = [UIFont systemFontOfSize:Zoom(12)];
            view.textColor = [RBThemeConfig textBlackColor];
            view.tag = 303;
            view.type = RBTextFieldTypeInt;
            view.minInt = 0;
            view.delegate = self;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:1 text:@"+"];
            view.tag = 304;
            [view rbAddTapGesWithTarget:self action:@selector(itemPlusBtnTapped:)];
            view;
        }) withHeight:CGFLOAT_MIN width:Zoom(30) top:0 bottom:0 left:0 right:0 centerY:0];
        view;
    }) withHeight:Zoom(30) width:Zoom(100) top:0 bottom:0 left:0 right:Zoom(12) centerY:1];
    [self configItemView:view withModel:model];
    return view;
}

@end
